<?php

class RoleController extends Controller
{



    /*
     * 获取角色列表
     *
     */
    public function actionGetRoleList()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();

            //初始化
            $searchsql = Util::initSearchSql();

          //  $searchsql = Util::getSerarchStr($searchsql,"mobile","mobile"); //手机号

            //获取count
            $totalcount = AdminRole::model()->count($searchsql);



            //排序
            $searchsql = $searchsql." order by role_id asc";
            //加上分页控制
            $searchsql = Util::addLimitSql($searchsql);

            //Util::responseMsg($searchsql);

            //获取list
            $smslist = AdminRole::model()->findAll($searchsql);



            // Util::responseObj(MsgConst::RETURN_SUCCESS);
            Util::responseList($totalcount,$smslist);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 创建角色
     *
     */
    public function actionnewRole()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $role_name = Util::getParamNotEmpty("role_name");

            //新增角色
            AdminRoleService::addNewRole($role_name);

            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 删除角色
     */
    public function actionDeleteRole()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $role_id = Util::getParamNotEmpty("role_id");

            //删除角色
            AdminRoleService::deleteRole($role_id);

            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
    * 修改角色信息
    */
    public function actionUpdateRole()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $role_id = Util::getParamNotEmpty("role_id");
            $role_name = Util::getParamNotEmpty("role_name");

            //删除角色
            AdminRoleService::updateRole($role_id,$role_name);

            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    /*
     * 获取角色对应的菜单信息
     */
    public function actionGetMenuInfo()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $role_id = Util::getParamNotEmpty("role_id");


            //初始化
            $searchsql = Util::initSearchSql();
            $returnlist = AdminMenuService::getMenuList($searchsql);

            for($i=0;$i<count($returnlist);$i++)
            {
                $returnlist[$i]->is_select = AdminRoleMenuService::checkMenuAndRole($returnlist[$i]->menu_id,$role_id);
            }


            //Util::responseObj($returnlist);

            Util::responseList(count($returnlist),$returnlist);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }

    //设置角色对应的菜单信息

    public function actionSetMenuInfo()
    {
        try
        {


            //检测
            $user_id = SessionService::getUserIDBySessionid();


            //参数选择
            $role_id = Util::getParamNotEmpty("role_id");



            $select_list = Util::getParam("select_list","");
            $unselect_list = Util::getParam("unselect_list","");

            if($select_list == "" && $unselect_list == "")
                throw new Exception("select_list 和 select_list 不能同时为空");

            //设置
            AdminRoleMenuService::setRoleMenuRelation($role_id,$select_list,$unselect_list);



            Util::responseMsg(MsgConst::RETURN_SUCCESS);


        }catch (Exception $e)
        {

            Util::responseErrorMsg($e->getMessage());

        }
    }



}